package cn.zkteco.doordu.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import cn.zkteco.doordu.R;
import cn.zkteco.doordu.model.Device;

/**
 * Created by Administrator on 2017/9/14.
 */

public class DeviceAdapter extends BaseAdapter {

    private final Context context;
    private final List<Device> device;
    private final LayoutInflater inflater;

    public DeviceAdapter(Context context, List<Device> device) {
        this.context = context;
        this.device = device;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return device.size();
    }

    @Override
    public Object getItem(int position) {
        return device.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View view, ViewGroup parent) {

        Holder holder;
        if (view == null) {
            view = inflater.inflate(R.layout.item_ble_device, null);
            holder = new Holder();
            holder.name = view.findViewById(R.id.device_name);
            holder.address = view.findViewById(R.id.device_address);
            holder.rssi = view.findViewById(R.id.txt_rssi);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }
        String name = device.get(position).name;
        holder.name.setText(name == null ? context.getString(R.string.unknown_device) : name);
        holder.address.setText(device.get(position).address);
        holder.rssi.setText(String.valueOf(device.get(position).rssi));
        return view;
    }

    static class Holder {
        TextView name, address, rssi, scanRecord;
    }
}
